﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VKlibs;
using System.Net;

namespace VKapp.UserControls
{
    public partial class MainControl : UserControl
    {
        private Audiolist songlist = null;
        

        public MainControl()
        {
            InitializeComponent();
        }

        void webBrowser_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
        {
            songlist = new Audiolist(webBrowser);
            clbAudioList.Items.Clear();
            foreach(Audio song in songlist)
            {
                clbAudioList.Items.Add(song.Name, false);
            }
        }

        private void bDownload_Click(object sender, EventArgs e)
        {
            List<Audio> downloadlist = new List<Audio>();
            foreach(object o in clbAudioList.CheckedItems)
            {
                String name = (String)o;
                
                foreach(Audio song in songlist)
                {
                    if (song.Name == name)
                        downloadlist.Add(song);
                }
            }

            FolderBrowserDialog fbg = new FolderBrowserDialog();
            fbg.ShowDialog();

            WebClient wc = new WebClient();

            foreach(Audio song in downloadlist)
            {
                String fullPath = fbg.SelectedPath+"\\"+song.Name+".mp3";
                wc.DownloadFile(song.Url, fullPath);
                MessageBox.Show("Download complete");
            }
        }      
    }
}
